(function(){
	var doc = window.document;
	function $id(id){ return doc.getElementById(id);}
	var start = new Datepicker($id('audi_time'), {eachW:206,ttMonth:2, addDays:60, callback:callnext}); //棣栨鐐瑰嚮寮�鏃ユ湡鍙兘閫夋嫨60澶�
	var firstVal = $id('audi_time').value;
	//console.log(firstVal);
	var today = new Date(firstVal.split('-')[0], firstVal.split('-')[1]-1, firstVal.split('-')[2]); //鍘诲紑濮嬫棩鏈熸椂闂翠綔涓虹粨鏉熸棩鏈熺殑璧峰鏃堕棿
	//console.log(today);
	var firststart = addDay(1, today).split('-');  //榛樿缁撴潫鏃ユ湡浠庡叆浣忔棩鏈熷悗涓�ぉ寮�
	//console.log(firststart);
	//var firststart = addDay(1, new Date()).split('-'); //榛樿缁撴潫鏃ユ湡浠庢槑澶╁紑濮�
	var days = Math.ceil((today-new Date())/86400000); //鑾峰彇寮�鏃ユ湡璺濈浠婂ぉ鐨勬椂闂村樊
	var firstend = addDay(60, new Date()).split('-'); //榛樿寮�鏃ユ湡鐨別ndDate
	//console.log(firststart);
	var end = new Datepicker($id('id_backDate'), {eachW:206,ttMonth:2, audi_time:[firststart[0], (firststart[1]-1), firststart[2]], addDays:parseInt(28+days), endDate:firstend, callback:callhotel});
	
	function callhotel(o, t){
	    o.value = t.title;
		
		var sd = $id('audi_time');
		getalldays(sd, o);
		
	}
	
	function getalldays(s,e){
		//var eo = $id('audi_time');
		if(!$id('daysOfStay')){return false;}
		var edd = new Date(e.value.split('-')[0], e.value.split('-')[1]-1, e.value.split('-')[2]);
		var sdd = new Date(s.value.split('-')[0], s.value.split('-')[1]-1, s.value.split('-')[2]);
		var days = Math.ceil((edd-sdd)/86400000);
		
		if(days < 0){
		    days = 0;
		}
		
		$id('daysOfStay').value = days;
	}
	
	
	function callnext(o, t){
	    o.value = t.title;
		var eo = $id('id_backDate');
		
		var sdd = new Date(o.value.split('-')[0], o.value.split('-')[1]-1, o.value.split('-')[2]);
		var edd = new Date(eo.value.split('-')[0], eo.value.split('-')[1]-1, eo.value.split('-')[2]);
	    //if(sdd > edd){
			eo.value = addDay(1, sdd);
			//eo.value = addDay(7, sdd);
		//}
		getalldays(o,eo);
		var days = Math.ceil((sdd-new Date())/86400000);
		
		//console.log(days);
		
		var setend = addDay(60, new Date()).split('-');
		
		//console.log(parseInt(28+days));
		//console.log(o.value);
		
		var _e_ = new Date(o.value.split('-')[0], (o.value.split('-')[1]-1), o.value.split('-')[2]);
		var _f_ = addDay(1, _e_).split('-');
		
		end = new Datepicker(eo, {eachW:206,ttMonth:2, audi_time:[_f_[0], (_f_[1]-1), _f_[2]], addDays:parseInt(28+days), endDate:setend, callback:callhotel});
	    window.setTimeout(function(){eo.focus();}, 0);
	}
	
	function addDay(num,date){
		var now = date ? new Date(date.getFullYear(), date.getMonth(), date.getDate()) : new Date();
		var olddate = now.getDate();
		now.setDate(now.getDate()+num);
		return now.getFullYear() + '-' + ((now.getMonth()+1)<10 ? "0"+(now.getMonth()+1): (now.getMonth()+1))+'-'+ (now.getDate()<10 ? "0"+now.getDate() :now.getDate());
	}	

	function getbyClass(parent, tag, name){
		parent = parent || document;
		tag = tag || "*";
		var arr = [];
		var reg = new RegExp("(^|\\s)" +name+ "(\\s|$)");
		var ts = parent.getElementsByTagName(tag);
		for(var i=0, j=ts.length; i<j; i++){
			 if(reg.test(ts[i].className)){
				arr.push(ts[i]);
			 }
		}
		return arr;

	}


	var inputs = getbyClass(document, 'input', 'h_datepikcer');
	//console.log(inputs);
	if(inputs && inputs.length){
		var iobj = eobj = {};
		for(var i=0,len=inputs.length; i<len; i++){
			var p = i;
			(function(){
			
				var iid = inputs[p].id;
				//console.log(iid);
				if(iid && iid.indexOf('audi_time')!= -1){
					var num = iid.slice(12);
					//console.log(num);
					
					//if($id('backDate_'+num)){
					var _stval = $id(iid).value.split('-');
					
					var _s_ = addDay(1, new Date(_stval[0],(_stval[1]-1),_stval[2])).split('-');
					//console.log(_s_);
					//var _setstart = [_stval[0], (_stval[1]-1), _stval[2]];
					var _setstart = [_s_[0], (_s_[1]-1), _s_[2]];
					var  _days = new Date(_stval[0], _stval[1]-1, _stval[2]);
					var _addday = Math.ceil((_days-new Date())/86400000);
					var _setend = addDay(60, new Date()).split('-');//$id(iid).value.split('-');
					//console.log(_setstart);
					eobj[num] = new Datepicker($id('id_backDate'+num), {eachW:206,ttMonth:2, audi_time:_setstart, addDays:parseInt(28+_addday), endDate:_setend, callback:callhotel});
					
					function dynamiccall(o, t){
						o.value = t.title;
						var eo = $id('id_backDate'+num);
						if(!eo){ return false;}
						var _sdd = new Date(o.value.split('-')[0], o.value.split('-')[1]-1, o.value.split('-')[2]);
						//var edd = new Date(eo.value.split('-')[0], eo.value.split('-')[1]-1, eo.value.split('-')[2]);
						//if(sdd > edd){
							eo.value = addDay(1, _sdd);
							//eo.value = addDay(7, sdd);
						//}
						var days = Math.ceil((_sdd-new Date())/86400000);
						//console.log(parseInt(7+days));
						//setend = $id(iid).value.split('-');
						//console.log(setend);
						var e_ = new Date(o.value.split('-')[0], (o.value.split('-')[1]-1), o.value.split('-')[2]);
						var f_ = addDay(1, e_).split('-');						
						eobj[num] = new Datepicker(eo, {eachW:206,ttMonth:2, audi_time:[f_[0], (f_[1]-1), f_[2]], addDays:parseInt(28+days), endDate:_setend,callback:callhotel});
					    window.setTimeout(function(){eo.focus();}, 0);
					}
					iobj[num] = new Datepicker($id(iid), {eachW:206,ttMonth:2, addDays:60, callback:dynamiccall});			
					
					//}
				}
				
			})();
		}
    }	

})();